<template>
  <AsyncSuspense :loader="loadControl" @loaded="emit('loaded', $event)">
    <template #default="{ data }">
      <slot :control="data"></slot>
    </template>
  </AsyncSuspense>
</template>

<script setup lang="ts">
import AsyncSuspense from '@/common/asyncSuspense.vue'
import { getControllerContext } from '@/controller/common/context'
import { onLoad } from '@dcloudio/uni-app'

const emit = defineEmits<{
  (e: 'loaded', control: any): void
}>()

async function loadControl() {
  return new Promise((resolve) => {
    onLoad((query) => {
      const haId = query?.haId || ''
      const control = getControllerContext(haId)
      resolve(control)
    })
  })
}
</script>
